﻿using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Models.SkillsCatalog;
using Portal.Logic.Services.Tests.Units;
using Portal.Logic.Validators.SkillsCatalog;
using Portal.Model;
using Portal.Repository.SkillsCatalog;
using Rhino.Mocks;
using Portal.Logic.Services.SkillsCatalog;
using System;

namespace Portal.Logic.Services.SkillsCatalog.Tests.Units
{
	[TestClass()]
	public class CurriculumVitaeServiceTests : ServiceBaseTests<CurriculumVitaeService, ICurriculumVitae, ICurriculumVitaeValidator, ISkillsCatalogUnitOfWorkProvider, ISkillsCatalogUnitOfWork>
	{
		private ICurriculumVitaeRepository _repository;

		protected override void InitializeTest()
		{
			base.InitializeTest();

			_repository = Mocks.DynamicMock<ICurriculumVitaeRepository>();
			Validator = Mocks.DynamicMock<ICurriculumVitaeValidator>();
			SetupResult.For(UnitOfWork.CurriculumVitaeRepository).Return(_repository);
		}

		#region GetBy

		[TestMethod()]
		public void GetBy_UserIsNull_ReturnsNull()
		{
			Mocks.ReplayAll();

			var service = GetService();
			var result = service.GetBy(null);

			result.Should().BeNull();
		}

		[TestMethod()]
		public void GetBy_UserIsValid_ReturnsCV()
		{
			var user = Mocks.DynamicMock<IUser>();
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			Expect.Call(_repository.GetByUser(0)).IgnoreArguments().Return(cv);
			Mocks.ReplayAll();

			var service = GetService();
			var result = service.GetBy(user);

			result.Should().Be(cv);
		}

		#endregion GetBy

		#region GetPreviewBy
		public void GetPreviewBy_UserIsNull_ReturnsNull()
		{
			Mocks.ReplayAll();

			var service = GetService();
			var result = service.GetPreviewBy(null);

			result.Should().BeNull();
		}

		[TestMethod()]
		public void GetPreviewBy_UserIsValid_ReturnsCV()
		{
			var user = Mocks.DynamicMock<IUser>();
			var cv = Mocks.DynamicMock<ICurriculumVitaePreview>();
			Expect.Call(_repository.GetPreviewByUser(0)).IgnoreArguments().Return(cv);
			Mocks.ReplayAll();

			var service = GetService();
			var result = service.GetPreviewBy(user);

			result.Should().Be(cv);
		}
		#endregion

		#region IsUpToDate
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void IsUpToDate_CVIsNull_ThrowsException()
		{
			Mocks.ReplayAll();

			var service = GetService();
			var result = service.IsUpToDate(null, DateTime.Now);
		}

		[TestMethod]
		public void IsUpToDate_IsNotOlderThan60Days_ReturnTrue()
		{
			var date = new DateTime(2000,01,01);
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			SetupResult.For(cv.UpdatedOn).Return(date.AddDays(-60));

			Mocks.ReplayAll();

			var service = GetService();
			var result = service.IsUpToDate(cv, date);

			result.Should().BeTrue();

		}

		[TestMethod]
		public void IsUpToDate_IsOlderThan60Days_ReturnFalse()
		{
			var date = new DateTime(2000, 01, 01);
			var cv = Mocks.DynamicMock<ICurriculumVitae>();
			SetupResult.For(cv.UpdatedOn).Return(date.AddDays(-61));

			Mocks.ReplayAll();

			var service = GetService();
			var result = service.IsUpToDate(cv, date);

			result.Should().BeFalse();
		}

		#endregion

		protected override CurriculumVitaeService GetService()
		{
			return new CurriculumVitaeService(Provider, Validator);
		}



	}
}